package com.kgc.web.servlet; /**
 * <h3>项目名称:demo03050</h3>
 * <p>本API描述:${description}</p>
 * <p>@author : AliyZhang</p>
 * <p>@date : 2021-03-08 12:36</p>
 **/

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "MyServlet",
        initParams = {@WebInitParam(name = "encoding",value = "UTF-8")},
        urlPatterns = {"*.do"},
        loadOnStartup = 1)
public class MyServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        //获取初始化参数
        String encoding = config.getInitParameter("encoding");
        System.out.println("调用了init方法。。。初始化参数encoding："+encoding);
    }

    @Override
    public void destroy() {
        System.out.println("调用了destroy方法。。");
    }

    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {
        System.out.println("调用了service方法。。。");
        //处理请求和响应中的乱码问题
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //接收用户传来的参数
        String key = request.getParameter("key");

        //把参数拼接后写到浏览器端
        PrintWriter writer = response.getWriter();

        String html="<p style='color:red;font-weight:bold;'>"+
                "服务器收到了："+key+"</p>";
        html+="<h1>哈哈</h1>";
        writer.write(html);
    }

}
